package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 搜索Controller.
 *
 * @author 宫新程
 * @since 2019/3/4 13:52
 */
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {

  @Resource private ArticleService articleService;

  /** 新增. */
  @PostMapping
  public Result save(@RequestBody Article article) {
    articleService.save(article);
    return new Result(true, StatusCode.OK, "保存成功");
  }

  /** 根据key搜索. */
  @GetMapping("/{page}/{size}")
  public Result findByKey(
      @RequestParam("key") String key, @PathVariable int page, @PathVariable int size) {
    Page<Article> pageInfo = articleService.findByKey(key, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<>(pageInfo.getTotalElements(), pageInfo.getContent()));
  }
}
